package com.itheima.sh.web;

import com.itheima.sh.pojo.User;
import com.itheima.sh.service.RegisterUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.处理请求乱码问题
        request.setCharacterEncoding("utf-8");
        //2.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //3.创建User对象
        User user = new User();
        //4.将请求参数封装到User对象中
        user.setUsername(username);
        user.setPassword(password);
        //5.创建service业务层对象
        RegisterUserServiceImpl userService = new RegisterUserServiceImpl();
        //6.使用业务层对象调用业务层的注册方法将User对象传递到业务层
        boolean f = userService.register(user);
        //将返回的值响应给前端
        response.getWriter().print(f);
    }
}